<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use app\common\model\Book as BookModel;
use app\common\model\Classes as ClassesModel;
use app\common\model\BookReadLog as BookReadLogModel;
use think\Db;
/**
 * Book 电子书控制器
 * Class Book
 * @package app\home\controller
 */
class Book extends Base
{
    /**
     * [getIndex  书本]
     * @param
     * @return json
     * @author [C。]
     */
    public function getIndex()
    {
        $param = input('param.');
        $this->getPageAndSize($param);
        $model = new BookModel();
        $cmodel = new ClassesModel;
        $map = [];
        $user = $this->user;
        try{
            $map['status'] = 1;
            $map['cateid'] = $param['cateid'];
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getlist($map, $this->from, $this->size);
            if(!empty($param['of'])){
                foreach ($lists as &$v){
                    $classes = $cmodel->findOne(['itemid'=>$v['id'],'user_id'=>$user['id'],'status'=>0,'of'=>$param['of']]);
                    if(!empty($classes)){
                        $v['is_like'] = 1;
                    }else{
                        $v['is_like'] = 2;
                    }
                }
            }
        }catch (\Exception $e) {
            systembug($e->getMessage(),2,'home/Book/getIndex');
            return admin_json(-1,'获取失败'.$e->getMessage());
        }
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [getIndex  获取本书所有章节]
     * @param
     * @return json
     * @author [C。]
     */
    public function getBook()
    {
        $param = input('param.');
        $model = new BookModel();
        try{
            $book = $model->findData($param['id'],'id');
        }catch (\Exception $e) {
            systembug($e->getMessage(),2,'home/Book/getBook');
            return admin_json(-1,'查看书本失败'.$e->getMessage());
        }
        return admin_json(1,'查看书本成功',$book);
    }
    /**
     * [getLogadd  添加读书记录]
     * @param   type 1早课进修 2晚课进修
     * @return json
     * @author [C。]
     */
    public function postLogadd()
    {
        $param = input('param.');
        $model = new BookReadLogModel();
        $user = $this->user;
        try{
            $param['mid'] = $user['id'];
            $log = $model->findOne(['mid'=>$user['id'],'bookid'=>$param['bookid']]);
            $param['content'] = serialize($param['content']);
            if(!empty($log)){
                $param['id'] = $log['id'];
                $param['status'] = 1;
                $model->editData($param);
            }else{
                $model->addData($param);
            }
            if(!empty($param['type'])){
                if($param['type'] == 1){
                    add_quality($user['id'],11,$user['quality'],2);
                }elseif($param['type'] == 2){
                    add_quality($user['id'],12,$user['quality'],2);
                }
            }
            Db::table('app_book')->where('id', $param['bookid'])->setInc('readnum');
        }catch (\Exception $e) {
            systembug($e->getMessage(),2,'home/Book/postLogadd');
            return admin_json(-1,'添加读书记录失败'.$e->getMessage());
        }
        return admin_json(1,'添加读书记录成功');
    }
    /**
     * [getLog  获取读书记录]
     * @param
     * @return json
     * @author [C。]
     */
    public function getLog()
    {
        $param = input('param.');
        $model = new BookReadLogModel();
        $user = $this->user;
        try{
            $log = $model->findOne(['mid'=>$user['id'],'bookid'=>$param['bookid']]);
            $log['content'] = unserialize($log['content']);
        }catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1,'获取记录失败'.$e->getMessage());
        }
        return admin_json(1,'获取记录成功',$log);
    }
}